#include "Ship.h"
#include "Weapon.h"
#include "GameManager.h"

using namespace irr;
using namespace core;
using namespace io;

void CShip::passTime(f32 frameDelta) {
	// accelerate us some more

	// let weapons parse our time too
	for(TContainer<IWeapon *>::iterator  iter = this->mWeaponList.begin(); iter != this->mWeaponList.end(); iter++)  {
		IWeapon *ptr = *iter;
		ptr->passTime(frameDelta);
	}
}

bool CShip::applyDamage(SArmor damage) {
	DEBUG_PRINT("Ship damage received\n");
	return false;
}
CShip::CShip(void) : TRefCount<CShip>() {

}

CShip::~CShip(void) {

}
void CShip::setAcceleration(f32 value) {
	if(this->mPhysxObject == NULL || value < 0)
		return;

	if(value > this->mMaxNormalAcceleration)
		value = this->mMaxNormalAcceleration;

	this->mAcceleration = value;
}
void CShip::setMass(f32 value) {
	if(this->mPhysxObject)
		this->mPhysxObject->setMass(value);
	this->mMass = value;
}
f32	 CShip::getMass(void) {
	if(this->mPhysxObject)
		mMass = this->mPhysxObject->getMass();
	
	return this->mMass;
}
void CShip::fireFullMount(E_WEAPON_MOUNTPOINT mount) {
	
	for(TContainer<IWeapon *>::iterator  iter = this->mWeaponList.begin(); iter != this->mWeaponList.end(); iter++)  {
		IWeapon *ptr = *iter;
		if(ptr->getMountPoint() == mount) {	
			ptr->fire();
		}
	}
}
void CShip::setFiringAtTarget(E_WEAPON_MOUNTPOINT mountPoint, vector3df target) {
	vector3df rot, offset;
	matrix4 m, n;

	switch(mountPoint) { 
		case E_WEAPON_MOUNTPOINT::Left:
			offset = vector3df(0,90,0);
			break;
		case E_WEAPON_MOUNTPOINT::Right:
			offset = vector3df(0,90,0);
			break;
		case E_WEAPON_MOUNTPOINT::Front:
			offset = vector3df(0,0,0);
			break;
		case E_WEAPON_MOUNTPOINT::Back:
			offset = vector3df(0,180,0);
			break;
		default:
			return;
	}
	rot = getRotationAtTarget(target);
	m.setRotationDegrees(rot);
    n.setRotationDegrees(offset);
    m *= n;
    this->setRotation( m.getRotationDegrees() ); 
}
void CShip::addWeapon(IWeapon *weapon) {
	weapon->setParent(this);
	this->mWeaponList.push_back(weapon);
}
CShip *CShip::copy(void) { 
	CShip *ptr = new CShip();

	ptr->setXmlID(this->getXmlID());
	ptr->setName(this->getName());
	ptr->setClass(this->getClass());

	ptr->setMaxNormalAcceleration(this->getMaxNormalAcceleration());
	ptr->setMaxHyperAcceleration(this->getMaxHyperAcceleration());
	ptr->setMeshPath(this->getMeshPath());

	ptr->setMesh(CGameManager::getInstance()->getSceneManager()->getMesh(this->getMeshPath()));
	ptr->setSceneNode(CGameManager::getInstance()->getSceneManager()->addMeshSceneNode(ptr->getMesh()));

	ptr->setPhysxMesh(CGameManager::getInstance()->getPhysxManager()->createConvexMesh(ptr->getMesh()->getMeshBuffer(0), vector3df(1,1,1)));
	ptr->setPhysxObject(CGameManager::getInstance()->getPhysxManager()->createConvexMeshObject(ptr->getPhysxMesh(), vector3df(0,0,0)));
	ptr->getPhysxObject()->setUserData(ptr);
	
	for(TContainer<IWeapon *>::iterator iter = this->mWeaponList.begin();iter != this->mWeaponList.end(); iter++) {
		IWeapon *temp = *iter;
		IWeapon *copy = temp->copy();
		copy->setParent(ptr);
		ptr->addWeapon(copy);
	}

	ptr->setMass(this->getMass());
	ptr->setPosition(this->getPosition());
	ptr->setAcceleration(this->getAcceleration());
	ptr->setVelocity(this->getVelocity());

	return ptr;
}